const mysql = require('../db/index')
//获取部门所有信息
exports.reqDeptUserInfo = ((req, res) => {
    const sql = 'select * from dept where dstatus = 0'
    mysql.query(sql, (err, result) => {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('部门信息查询失败')
        const total = result.length
        //分页查询
        const sql = 'select * from dept where dstatus = 0 limit ?,?'
        const page = parseInt(req.params.page)
        const limit = parseInt(req.params.limit)
        //这一步很重要
        const skPage = (page - 1) * limit
        mysql.query(sql, [skPage, limit], (err, result) => {
            if (err) return res.cc(err)
            if (result.length < 0) return res.cc('部门信息查询失败')
            res.send({ status: 0, message: '部门信息获取成功', data: result, total: total, page: page, limit: limit })
        })
    })
})
//新增部门信息
exports.reqAddDept = (req, res) => {
    const sql = 'select * from dept where dname = ?'
    mysql.query(sql, req.body.dname, (err, result) => {
        if (err) return res.cc(err)
        if (result.length == 1) return res.cc('部门名已存在，请重试输入！')
        //部门名可用
        const sql = 'insert into dept set ?'
        mysql.query(sql, req.body, (err, result) => {
            if (err) return res.cc(err)
            if (result.affectedRows != 1) return res.cc('新增部门失败！')
            res.cc('新增部门成功！', 0)
        })
    })
}
//查询正在使用的部门
exports.reqSelectUseDept = (req, res) => {
    const sql = 'select * from dept where dstatus = 0'
    mysql.query(sql, (err, result) => {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败，请重新输入！')
        const total = result.length
        //分页查询
        const sql = 'select * from dept where dstatus = 0 limit ?,?'
        const page = parseInt(req.params.page)
        const limit = parseInt(req.params.limit)
        //这一步很重要
        const skPage = (page - 1) * limit
        mysql.query(sql, [skPage, limit], (err, result) => {
            if (err) return res.cc(err)
            if (result.length < 0) return res.cc('查询失败，请重新输入！')
            res.send({ status: 0, message: '查询成功', data: result, total: total, page: skPage, limit: limit , use:0 })
        })
    })
}
//查询未使用的部门
exports.reqSelectDontUseDept = (req, res) => {
    const sql = 'select * from dept where dstatus = 1'
    mysql.query(sql, (err, result) => {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败，请重新输入！')
        const total = result.length
        //分页查询
        const sql = 'select * from dept where dstatus = 1 limit ?,?'
        const page = parseInt(req.params.page)
        const limit = parseInt(req.params.limit)
        //这一步很重要
        const skPage = (page - 1) * limit
        mysql.query(sql, [skPage, limit], (err, result) => {
            if (err) return res.cc(err)
            if (result.length < 0) return res.cc('查询失败，请重新输入！')
            res.send({ status: 0, message: '查询成功', data: result, total: total, page: skPage, limit: limit , use:1 })
        })
    })
}
//部门编辑
exports.reqEditDept = (req, res) => {
    const sql = 'update dept set ? where did = ?'
    mysql.query(sql, [req.body,req.body.did], (err, result) => {
        console.log(err);
        if (err) return res.cc(err)
        if (result.affectedRows != 1) return res.cc('部门编辑失败，请重新输入！')
        res.cc('部门编辑成功！', 0)
    })
}
//部门删除
exports.reqDeleteDept = (req, res) => {
    const sql = 'update dept set dstatus = 1 where did = ?'
    mysql.query(sql, req.params.did, (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows != 1) return res.cc('部门删除失败，请重新输入！')
        res.cc('部门删除成功！', 0)
    })
}
//撤销部门删除
exports.reqDontDeleteDept = (req, res) => {
    const sql = 'update dept set dstatus = 0 where did = ?'
    mysql.query(sql, req.params.did, (err, result) => {
        if (err) return res.cc(err)
        if (result.affectedRows != 1) return res.cc('撤销部门失败，请重新输入！')
        res.cc('撤销部门成功！', 0)
    })
}